﻿//-------------------------------------------------------------------------
///-----------------ipc_each_session---------------------------------------
///-----------------进程通讯会话类-----------------------------------------
///-----------------2022/3/28---by mky.蒙克雨------------------------------
//-------------------------------------------------------------------------
#include "common/global_include.h"
#include "file_mgr.h"
#include <stdio.h>
#include <unistd.h>
#include <vector>
#include <sys/stat.h>
#include <regex>

#define MAX_CURRENT_PATH_LEN 256 
//-------------------------------------------------------------------------
mv::FileMgr::FileMgr() {
	m_pCurPath = "";
	InfoL;
}

//-------------------------------------------------------------------------
mv::FileMgr::~FileMgr() {
	InfoL;
}

//-------------------------------------------------------------------------
std::string mv::FileMgr::getCurPath() {
	if(m_pCurPath!="")
		return m_pCurPath;
	m_pCurPath = getcwd(NULL, 0);
	InfoL << "文件路径:" << m_pCurPath ;
	//将需要调用的模块使用 strcat 作拼接;
	//const char* model_path = strcat(buffer, "/models");
	return m_pCurPath;
}
/**
 * @brief  string类型字符串分割函数
 * @param  str(string): 待分割字符串
 * @param  separator(string): 分割符号
 * @return result: 分割结果vetor容器,返回最后一个
 */
std::string mv::FileMgr::getLastElement(std::string str, const std::string& separator)
{
	std::vector<std::string> result;
	int cutAt;
	while ((cutAt = str.find_first_of(separator)) != str.npos)
	{
		if (cutAt > 0)
		{
			result.push_back(str.substr(0, cutAt));
		}
		str = str.substr(cutAt + 1);
	}
	if (str.length() > 0)
	{
		result.push_back(str);
	}
	int num = -1;
	num = result.size() - 1;
	if (num >= 0)
		return std::move(result[num]);

	return "";
}

/**
 * @brief  string类型字符串分割函数
 * @param  str(string): 待分割字符串
 * @param  separator(string): 分割符号
 * @return result: 分割结果vetor容器
 */
std::string mv::FileMgr::getFirstElement(std::string str, const std::string& separator)
{
	std::vector<std::string> result;
	int cutAt;
	while ((cutAt = str.find_first_of(separator)) != str.npos)
	{
		if (cutAt > 0)
		{
			result.push_back(str.substr(0, cutAt));
		}
		str = str.substr(cutAt + 1);
	}
	if (str.length() > 0)
	{
		result.push_back(str);
	}
	int num = -1;
	num = 0;
	if (num >= 0)
		return std::move(result[num]);

	return "";
}
//-------------------------------------------------------------------------
std::string mv::FileMgr::getMd5ValueByMd5File(std::string file) {
	ifstream infile;
	InfoL << "getMd5ValueByMd5File:"<< file;
	infile.open(file, ios::in);
	if (!infile.is_open())
	{
		ErrorL << "读取文件失败";
		return "";
	}
	//第三种读取方法
	std::string buf;
	getline(infile, buf);
	InfoL << buf;
	return std::move(buf);
}

//-------------------------------------------------------------------------
int mv::FileMgr::getFileSize(const std::string& fname)
{
	struct stat statbuf;
	if (stat(fname.c_str(), &statbuf) == 0)
		return statbuf.st_size;
	return -1;
}

//-------------------------------------------------------------------------

//正则表达式切割字符串
std::vector<std::string> mv::FileMgr:: stringSplit(const std::string& str, char delim)
 {
    std::string s;
    s.append(1, delim);
    std::regex reg(s);
    std::vector<std::string> elems(std::sregex_token_iterator(str.begin(), str.end(), reg, -1),
                                   std::sregex_token_iterator());
    return elems;
}

